# 5.4 Generator and JavaBean

# 1. Using the Generator

The ActiveRecord module of JFinal provides a Generator utility class within the com.jfinal.plugin.activerecord.generator package. This can automatically generate Model, BaseModel, MappingKit, and DataDictionary files.

After generation, the Model will merge with the JavaBean, immediately obtaining getter and setter methods, adhering to traditional JavaBean standards. This provides all the advantages of traditional JavaBeans, removing the need to remember column names during development.

Here's an example of how to use the generator:

// The package used by the model (MappingKit uses the default package)
String modelPackageName = "com.xxx.common.model";
		
// The package used by the base model
String baseModelPackageName = modelPackageName + ".base";
		
// Path to save the base model file
String baseModelOutputDir = System.getProperty("user.dir") + "/src/main/java/" + baseModelPackageName.replace('.', '/');
		
// Path to save the model file (MappingKit and DataDictionary files use the default save path)
String modelOutputDir = baseModelOutputDir + "/..";
		
System.out.println("Output path: "+ baseModelOutputDir);
 
// Create the generator
Generator gen = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
 
// Set the database dialect
gen.setDialect(new MysqlDialect());
 
// Generate field comments in the getter and setter methods
gen.setGenerateRemarks(true);
 
// Start generating the code
gen.generate();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

The parameters baseModelPackageName, baseModelOutputDir, modelPackageName, and modelOutputDir represent the package name of the base model, the output path of the base model, the package name of the model, and the output path of the model, respectively. Typically, only the modelPackageName needs to be configured, as the other parameters can be generated based on it.

It's recommended to download the jfinal-demo project from the official JFinal website, which provides a ready-to-use generator: http://www.jfinal.com

The generator has various setter configuration methods that allow for easy control over the generated results. For instance:

// Specify the template file for generating the base model
generator.setBaseModelTemplate(...);
 
// Specify the template file for generating the model
generator.setModelTemplate(...);
 
// Specify the template file for generating the MappingKit
generator.setMappingKitTemplate(...)
1
2
3
4
5
6
7
8
  • BaseModel: A base class meant to be extended by the final Model. All getter and setter methods are generated in this file. BaseModel is automatically generated and doesn't require manual maintenance. If the database structure changes, simply regenerate it.

  • MappingKit: Generates the mapping between tables and Models. It also generates configurations for primary and composite keys, eliminating the need for any boilerplate mapping code in configPlugin(Plugins me).

  • DataDictionary: This generates a data dictionary that provides details about all the fields in a database table, such as field name, type, length, remarks, and whether it's a primary key.

# 3. Filtering and Excluding Tables

From JFinal 5.0.0, the Generator provides addBlacklist(blacklist) and addWhitelist(whitelist) methods to add tables to a whitelist or blacklist. Tables in the blacklist will be excluded from generation, while only those in the whitelist will be generated.

For versions prior to JFinal 5.0.0, a custom filtering mechanism was used.

# 4. Main Advantages of Using the Generator

  • Utilize a vast array of third-party tools designed for Beans, like Jackson and Freemarker.

  • Rapidly adapt to database table changes, streamline refactoring, improve development efficiency, and enhance code quality.

  • No need to remember database column names, thanks to IDE code hints, reducing cognitive overhead and preventing typos.

  • BaseModel design ensures the final Model remains clean and is quickly refactored when table structures change.

  • Automatic table-to-Model mapping.

  • Automatic primary and composite key name identification and mapping.

  • The MappingKit contains all mapping code, keeping JFinalConfig clean and concise.

# 5. Considerations after Merging Model with Bean

  • JSP templates will rely on the Bean's getter method for output.

  • In the Controller, getModel() requires form field names to correspond to database column names. Meanwhile, getBean() relies on setter methods.

  • Many third-party tools like Jackson and Fastjson rely on the Bean's getter methods. Hence, only after merging can one use Jackson or Fastjson.

  • When converting a Model to JSON data using JFinalJson, the JSON's keyName is the original database column name. In contrast, tools like Jackson or Fastjson will convert column names to camelCase based on the getter methods.

# 6. Common Issues

When using the generator with the SQL Server database, system tables might be fetched. These tables should be filtered out. The specific method can be found at: http://www.jfinal.com/share/211.

Last Updated: 9/17/2023, 6:16:57 AM